/**
 * 
 */
package com.study.io.netty;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

/**
 * @author Administrator
 * @date 2019年6月9日
 */
public class ClientChannelHander extends ChannelInboundHandlerAdapter {

	/*捕获数据在通道传输过程的异常*/
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        //super.exceptionCaught(ctx, cause);
        System.out.println("错误："+cause.getMessage());
    }
    /*接收数据*/
//    @Override
//    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
//        
//        System.out.println("client receive msg: " + msg.toString());
//    }
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        System.out.println("client receive msg:"+msg.toString());
    }
    /*链接服务器时发送数据*/
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        super.channelActive(ctx);
        ByteBuf buf = Unpooled.buffer();
        buf.writeBytes("客户端建立连接！".getBytes());
        ctx.writeAndFlush(buf);
    }
    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("连接关闭! ");
        super.channelInactive(ctx);
    }
}
